package com.qingcloud.common.job.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;


/**
 * XXL-Job 配置类
 */


@AutoConfiguration
@ConditionalOnClass(XxlJobSpringExecutor.class)
@EnableScheduling
@Configuration
@EnableConfigurationProperties(JobProperties.class)
@Slf4j
public class XxlJobConfig {

    @Bean
    @ConditionalOnMissingBean
    public XxlJobExecutor xxlJobExecutor(JobProperties properties) {
        log.info("初始化XXL-Job执行器的配置");
        JobProperties.AdminProperties admin = properties.getAdmin();
        JobProperties.ExecutorProperties executor = properties.getExecutor();
        // 初始化执行器
        XxlJobExecutor xxlJobExecutor = new XxlJobSpringExecutor();
        xxlJobExecutor.setIp(executor.getIp());
        xxlJobExecutor.setPort(executor.getPort());
        xxlJobExecutor.setAppname(executor.getAppName());
        xxlJobExecutor.setLogPath(executor.getLogPath());
        xxlJobExecutor.setLogRetentionDays(executor.getLogRetentionDays());
        xxlJobExecutor.setAdminAddresses(admin.getAddresses());
        xxlJobExecutor.setAccessToken(properties.getAccessToken());
        return xxlJobExecutor;
    }

}
